//
//  Three3DGroup.m
//  WangLottery
//
//  Created by 黄修勇 on 12-4-27.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "Three3DGroup.h"
#import <QuartzCore/QuartzCore.h>
#import "BellButton.h"

@implementation Three3DGroup

@synthesize lotteryType=_lotteryType;
@synthesize minSelectNumber=_minSelectNumber;
@synthesize selectNumberArray=_selectNumberArray;
@synthesize titleString=_titleString;

-(void)dealloc{

    [selectNums release];
    [selectView release];
    [selectBellArray release];
    [titleLabel release];
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [self initWithFrame:frame andLotteryType:Three3DGroupTypeSan];
    if (self) {
        // Initialization code
    }
    return self;
}

-(id)initWithFrame:(CGRect)frame andLotteryType:(Three3DGroupType)type{

    self=[super initWithFrame:frame];
    if (self) {
        
        _lotteryType=type;
        
        selectNums=[NSMutableArray new];
        selectBellArray=[NSMutableArray new];

        self.layer.backgroundColor=[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.4f].CGColor;
        self.layer.cornerRadius=6;
        
        selectView=[[UIScrollView alloc]initWithFrame:CGRectMake(10.0f, 15.0f, frame.size.width-20.0f, 50.0f)];
        selectView.layer.borderColor=[UIColor redColor].CGColor;
        selectView.layer.borderWidth=1.0f;
        selectView.layer.cornerRadius=6;
        [self addSubview:selectView];
        
        titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(10.0f, selectView.frame.origin.y+selectView.frame.size.height+10.0f, selectView.frame.size.width, 20.0f)];
        titleLabel.backgroundColor=[UIColor clearColor];
        titleLabel.textAlignment=UITextAlignmentCenter;
        [self addSubview:titleLabel];
        titleLabel.font=[UIFont systemFontOfSize:16.0f];
        
        float margin=(frame.size.width-50.0f-5*30.0f)/4;
        for (int i=0; i<10; i++) {
            
            NSInteger lines=i/5;
            NSInteger count=i-lines*5;
            
            BellButton *btn=[BellButton creatBellWithFrame:CGRectMake(25.0f+count*(30.0f+margin), lines*(30.0f+20.0f)+110.0f, 30.0f, 30.0f)];
            btn.number=i;
            btn.bellColor=BellColorRed;
            btn.tag=i+10;
            btn.bellState=BellStateNormal;
            [btn addTarget:self action:@selector(bellDone:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview: btn];
            
        }
        if (type==Three3DGroupTypeSan) {
            _minSelectNumber=2;
            [self setTitleString:@"至少选2个号,单注最高奖金320元"];
        }else {
            _minSelectNumber=3;
            [self setTitleString:@"至少选3个号，单注最高奖金160元"];
        }
    }
    return self;
}

-(void)setTitleString:(NSString *)titleString{

    titleLabel.text=titleString;
}

-(NSString *)titleString{

    return titleLabel.text;
}

-(NSArray *)selectNumberArray{

    return [NSArray arrayWithArray:selectNums];
}

-(void)bellDone:(BellButton *)btn{

    if (btn.bellState==BellStateNormal) {
        if ([self addNumber:btn.number]) {
            btn.bellState=BellStateSelct;
        }
    }else {
        [self removeNumber:btn.number];
        btn.bellState=BellStateNormal;
    }
}

-(void)bellDelete:(BellButton *)btn{

    [btn removeFromSuperview];
    [self removeNumber:btn.number];
    [selectBellArray removeObject:btn];
    for (BellButton *newBtn in self.subviews) {
        if ([newBtn isKindOfClass:[BellButton class]]&&newBtn.number==btn.number) {
            newBtn.bellState=BellStateNormal;
        }
    }
}

-(BOOL)addNumber:(NSInteger)number{

    NSNumber *num=[NSNumber numberWithInt:number];
   /* if ([selectNums containsObject:num]) {
        return NO;
    }*/
    BellButton *btn=[BellButton creatBellWithFrame:CGRectMake(10.0f+selectNums.count*(30.0f+10.0f), 10.0f, 30.0f, 30.0f)];
    btn.bellColor=BellColorRed;
    btn.bellState=BellStateSelct;
    [btn addTarget:self action:@selector(bellDelete:) forControlEvents:UIControlEventTouchUpInside];
    btn.number=number;
    [selectView addSubview:btn];
    selectView.contentSize=CGSizeMake(btn.frame.origin.x+btn.frame.size.width+10.0f, selectView.frame.size.height);
    [selectNums addObject:num];
    [selectBellArray addObject:btn];
    [self reloadBellNumber];
    return YES;
}

-(BOOL)removeNumber:(NSInteger)number{
    
    NSNumber *num=[NSNumber numberWithInt:number];
    if (![selectNums containsObject:num]) {
        return NO;
    }
    [selectNums removeObject:num];
    [[selectBellArray lastObject]removeFromSuperview];
    [selectBellArray removeLastObject];
    [self reloadBellNumber];
    return YES;
}

-(void)removeAllNumber{

    for (UIView *view in selectBellArray) {
        [view removeFromSuperview];
    }
    [selectBellArray removeAllObjects];
    [selectNums removeAllObjects];
}

-(void)reloadBellNumber{

    [selectNums sortUsingSelector:@selector(compare:)];
    NSLog(@"select Numbers %@",selectNums);
    for (int i=0; i<selectNums.count; i++) {
        NSInteger number=[[selectNums objectAtIndex:i] intValue];
        BellButton *btn=[selectBellArray objectAtIndex:i];
        btn.number=number;
    }
    [selectView scrollRectToVisible:CGRectMake(selectView.contentSize.width-selectView.frame.size.width, 0.0f, selectView.frame.size.width, selectView.frame.size.height) animated:YES];
}

- (void)removeAllselect{

    for (UIView *v in selectBellArray) {
        [v removeFromSuperview];
    }
    
    [selectBellArray removeAllObjects];
    [selectNums removeAllObjects];

    
  for (int i=0; i<10; i++) {
  
      BellButton *btn=(BellButton *)[self viewWithTag:i+10];
      
      btn.bellState=BellStateNormal;
  
  }

}

- (void)randomSelect3Direct
{

    [self removeAllNumber];
    [self removeAllselect];
    [self reloadBellNumber];
    
    if (_lotteryType == Three3DGroupTypeSan) {
        
        int index1 = arc4random() % 9;
        int index;
        
        BOOL marks=YES;
        while (marks) {
            index = arc4random() % 9;
            
            // 标记和第一次随机的不一样
            if (index != index1) {
                marks=NO;
            }
            
        }
        
        [self addNumber:index1];
        [self addNumber:index1];
        BellButton *btn1=(BellButton *)[self viewWithTag:index1+10];
        btn1.bellState=BellStateSelct;
        
        [self addNumber:index];
        BellButton *btn=(BellButton *)[self viewWithTag:index+10];
        btn.bellState=BellStateSelct;
        
        
    }
    else if (_lotteryType == Three3DGroupTypeLiu) {
        
        NSMutableArray *redRandomBall=[[NSMutableArray alloc] init];
        
        //机选 随机红球
        for (int i=0; i<3; i++) {
            
            int index = arc4random() % 9;
            
            BOOL mark=YES; // 查找是否生成相同的标记
            for (int j=0; j<[redRandomBall count]; j++) {
                
                int index2=[[redRandomBall objectAtIndex:j] intValue];
                
                if (index2 == index) {
                    mark=NO;
                    i--;
                    break;
                }
            }
            if (mark) [redRandomBall addObject:[NSNumber numberWithInt:index]];
            
      }
        
        for (int j=0; j<[redRandomBall count]; j++) {
            
            NSInteger indextag=[[redRandomBall objectAtIndex:j] intValue];
            
            [self addNumber:indextag];
            
            BellButton *btn=(BellButton *)[self viewWithTag:indextag+10];
            btn.bellState=BellStateSelct;
            
        }
        
        [redRandomBall removeAllObjects];
        [redRandomBall release];
    
     }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
